
/**
 * A dep is an observable that can have multiple
 * directives subscribing to it.
 */
/**
 * 此类用来收集依赖Wather
 */

export default class Dep{
    static target;
    subs = [];

    constructor(){
        this.subs = [];
    }

    addSub(sub){
        this.subs.push(sub);
    }

    // 删除依赖
    removeSub(sub){
        remove(this.sub,sub);
    }

    // 添加依赖
    depend(){
        if(Dep.target){
            this.addSub(Dep.target);
        }
    }

    // 通知所有依赖更新
    notify(){
        const subs = this.subs.slice();
        for(let i = 0; i < subs.length; i++){
            subs[i].update();
        }
    }
}

// 删除数组中的一个指定元素
export function remove (arr, item){
    if (arr.length) {
        const index = arr.indexOf(item)
        if (index > -1) {
            return arr.splice(index, 1)
        }
    }
}